package com.shixulin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shixulin.domain.ResponseResult;
import com.shixulin.entity.Course;
import com.shixulin.entity.DTO.NewsQueryDTO;
import com.shixulin.entity.News;
import com.shixulin.mapper.NewsMapper;
import com.shixulin.service.NewsService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (News)表服务实现类
 *
 * @author makejava
 * @since 2023-11-17 12:12:04
 */
@Service("newsService")
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Override
    public ResponseResult uploadNews(News news) {

        try{
            int insert = this.baseMapper.insert(news);
            if(insert>0){
                return new ResponseResult(300,"上传成功");
            }else {
                return new ResponseResult(501,"上传失败");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(501,"上传失败",e);
        }
    }

    @Override
    public ResponseResult getNews( NewsQueryDTO newsQueryDTO) {
//        List<News> news = this.baseMapper.selectList(null);
        try{
            Page<News> rowPage = new Page(newsQueryDTO.getCurrent(), newsQueryDTO.getSize());
            //queryWrapper组装查询where条件
            LambdaQueryWrapper<News> queryWrapper = new LambdaQueryWrapper<>();
            rowPage = this.baseMapper.selectPage(rowPage, queryWrapper);
            if(rowPage != null){
                return new ResponseResult(300,"查询成功",rowPage);
            }else {
                return new ResponseResult(505,"查询失败");
            }
        }catch(Exception e){
            e.printStackTrace();
            return new ResponseResult(503,"错误");
        }

    }

    @Override
    public ResponseResult selectOneNews(int id) {
        return  new ResponseResult(300,"查询成功", this.baseMapper.selectById(id));
    }

    @Override
    public ResponseResult updateNews(News news) {

        try{
            int insert = this.baseMapper.updateById(news);
            if(insert>0){
                return new ResponseResult(300,"修改成功");
            }else {
                return new ResponseResult(505,"修改失败");
            }
        }catch (Exception e) {
            return new ResponseResult(505,"错误");
        }

    }

    @Override
    public ResponseResult deleteNews(int id) {
        try{
            int deleteByPrimaryKey = this.baseMapper.deleteById(id);
            if (deleteByPrimaryKey > 0){
                return new ResponseResult(300,"删除成功");

            }else {
                return new ResponseResult(505,"删除失败");
            }
        }
        catch (Exception e) {
            return new ResponseResult(404,"错误");
        }
    }

    @Override
    public ResponseResult getNews1() {
        List<News> news = this.baseMapper.selectList(null);
        try{
            if(news!=null){
                return new ResponseResult(300,"查询成功",news);
            }else {
                return new ResponseResult(504,"查询失败");
            }
        }catch (Exception e) {
            return new ResponseResult(505,"错误");
        }
    }
}
